package com.java.business.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;

import cn.hutool.core.util.StrUtil;
import com.java.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.java.business.entity.Teacher;
import com.java.business.service.TeacherService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;



/**
 * 讲师
 *
 * @author jl
 * @since 2020-10-07 10:31:35
 */
@RestController
@RequestMapping("admin/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/all")
    public R allTeacher() {
        List<Teacher> teachers = teacherService.list();
        return R.ok(teachers);
    }

    /**
     * 带条件分页查询讲师
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = teacherService.queryPage(params);

	    return R.ok(page);
    }


    /**
     * 根据主键查询讲师
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		Teacher teacher = teacherService.getById(id);

        return R.ok(teacher);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R save(@RequestBody Teacher teacher) {
        if (StrUtil.isNotBlank(teacher.getId())) {
            teacherService.updateById(teacher);
        }else {
            teacher.setId(UuidUtil.getShortUuid());
            teacherService.save(teacher);
        }
        return R.ok(teacher);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        teacherService.removeById(id);
        return R.ok();
    }

}
